package gui;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JSplitPane;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.SpinnerDateModel;
import javax.swing.event.ListDataListener;

import fachlogik.Krankenhausverwaltung;
import fachlogik.Personal;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;

public class PersonalPanel extends JPanel
{
	private JTextField tfVorname;
	private JTextField tfName;
	private JTextField tfSvnr;
	private JTextField tfId;
	private JList<Personal> list;
	private MyListModel model;
	private JButton btnHinzufgen;
	
	private Krankenhausverwaltung verwaltung;
	private SpinnerDateModel gebdat = new SpinnerDateModel();
	private boolean editorOpen = false;

	/**
	 * Create the panel.
	 */
	public PersonalPanel(Krankenhausverwaltung ver)
	{
		this.verwaltung = ver;
		
		setLayout(new BorderLayout(0, 0));
		
		JSplitPane splitPane = new JSplitPane();
		add(splitPane, BorderLayout.CENTER);
		
		JPanel panel_1 = new JPanel();
		panel_1.setLayout(new BorderLayout(0, 0));
		splitPane.setLeftComponent(panel_1);
		
		JPanel panel = new JPanel();
		panel_1.add(panel, BorderLayout.NORTH);
		panel.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel lblVorname = new JLabel("Vorname:");
		panel.add(lblVorname);
		
		tfVorname = new JTextField();
		panel.add(tfVorname);
		tfVorname.setColumns(10);
		
		JLabel lblName = new JLabel("Name:");
		panel.add(lblName);
		
		tfName = new JTextField();
		panel.add(tfName);
		tfName.setColumns(10);
		
		JLabel lblGebdatum = new JLabel("GebDatum:");
		panel.add(lblGebdatum);
		
		JSpinner spinner = new JSpinner(gebdat);
		spinner.setEditor(new JSpinner.DateEditor(spinner, "dd.MM.yyyy"));
		panel.add(spinner);
		
		JLabel lblSvnr = new JLabel("SVNR:");
		panel.add(lblSvnr);
		
		tfSvnr = new JTextField();
		panel.add(tfSvnr);
		tfSvnr.setColumns(10);
		
		JLabel lblId = new JLabel("Personal-ID:");
		panel.add(lblId);
		
		tfId = new JTextField();
		panel.add(tfId);
		tfId.setColumns(10);
		
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2, BorderLayout.CENTER);
		
		btnHinzufgen = new JButton("Hinzuf\u00FCgen");
		btnHinzufgen.addActionListener(new ActionListener() {
			
		
		public void actionPerformed(ActionEvent arg0) {
				String vorname = tfVorname.getText();
				String name = tfName.getText();
				Date gebdatum = gebdat.getDate();
				String svnr = tfSvnr.getText();
				String id = tfId.getText();
				try{
					verwaltung.personalhinzu(vorname, name, gebdatum, svnr, id);
					list.updateUI();
				}
				catch(Exception e){
					JOptionPane.showMessageDialog(PersonalPanel.this, e.getMessage());
				}
			}
		});
		panel_2.add(btnHinzufgen);
		
		JScrollPane scrollPane = new JScrollPane();
		splitPane.setRightComponent(scrollPane);
		
		model = new MyListModel();
		list = new JList<Personal>(model);
		list.setCellRenderer(new MyRenderer());
		scrollPane.setViewportView(list);

	}
	
	private class MyListModel implements ListModel<Personal>{

		@Override
		public void addListDataListener(ListDataListener arg0)
		{
			// TODO Auto-generated method stub
			
		}

		@Override
		public Personal getElementAt(int arg0)
		{
			return verwaltung.getPersonalliste().get(arg0);
		}

		@Override
		public int getSize()
		{
			return verwaltung.getPersonalliste().size();
		}

		@Override
		public void removeListDataListener(ListDataListener arg0)
		{
			// TODO Auto-generated method stub
			
		}
		
	}
	
	private class MyRenderer implements ListCellRenderer<Personal>{

		@Override
		public Component getListCellRendererComponent(
				JList<? extends Personal> jlist, Personal value, int index,
				boolean selected, boolean focus)
		{
			JLabel label = new JLabel(""+value);
			label.setOpaque(true);
			if(selected)
			{
				label.setBackground(jlist.getSelectionBackground());
				label.setForeground(jlist.getSelectionForeground());
				editorOpen = false;
				list.setFocusable(true);
			}
			else
			{
				label.setBackground(jlist.getBackground());
				label.setForeground(jlist.getForeground());
			}
			
			if(focus){
				if(!editorOpen)
				{
					PersonalEditor editor = new PersonalEditor(verwaltung, value, list);
					list.clearSelection();
					list.setFocusable(false);
					editorOpen = true;
				}
			}
			
			return label;
		}
		
	}

}
